*Write a permanent log file;
PROC PRINTTO LOG="C:\Research\Alcohol and Morbidity\7. Code Archive\Data Files and Code That Produced Them\P02 ED Create Population Counts.log" NEW;
RUN;

*Write a permanent list file;
PROC PRINTTO PRINT="C:\Research\Alcohol and Morbidity\7. Code Archive\Data Files and Code That Produced Them\P02 ED Create Population Counts.lst" NEW;
RUN;

/*
This program creates population denominators for the month level counts of Emergency Department Admissions
1. All AZ, NJ and WI residents 19, 20, 21 or 22 years of age and by gender
2. By State for AZ, CA, IA, NJ, WI
3. It uses the same denominator for all ages but stacking the years means this is not much of an issue as the difference between the number of 20 to 22 year olds after the stacking is
   only one tenth of a percent with there being slightly more 22 year olds so it twists the age profile very slightly clockwise. 
*/

libname pop "D:\Data\Population Data\Populations Data US, State, County\By State Age and Sex 1970-1999\Census Reports";
libname inter "D:\Data\inter";


%macro pop_c(year,i_file,state1,state2,state3);
*Count everyone then males then females;
proc means data = pop.&i_file. noprint;
   var POPESTIMATE&year.;
   output out = junk1 sum = all&year.;
   where NAME in ("&state1.","&state2.","&state3.") and age in (19,20,21,22) and sex = 0;
run;
proc means data = pop.&i_file. noprint;
   var POPESTIMATE&year.;
   output out = junk2 sum = male&year.;
   where NAME in ("&state1.","&state2.","&state3.") and age in (19,20,21,22) and sex = 1;
run;
proc means data = pop.&i_file. noprint;
   var POPESTIMATE&year.;
   output out = junk3 sum = female&year.;
   where NAME in ("&state1.","&state2.","&state3.") and age in (19,20,21,22) and sex = 2;
run;
*Pull the by state numbers for the robustness;
proc means data = pop.&i_file. noprint;
   var POPESTIMATE&year.;
   output out = junk4 sum = all_AZ&year.;
   where NAME = "Arizona" and age in (19,20,21,22) and sex = 0;
run;
proc means data = pop.&i_file. noprint;
   var POPESTIMATE&year.;
   output out = junk5 sum = all_CA&year.;
   where NAME = "California" and age in (19,20,21,22) and sex = 0;
run;
proc means data = pop.&i_file. noprint;
   var POPESTIMATE&year.;
   output out = junk6 sum = all_IA&year.;
   where NAME = "Iowa" and age in (19,20,21,22) and sex = 0;
run;
proc means data = pop.&i_file. noprint;
   var POPESTIMATE&year.;
   output out = junk7 sum = all_NJ&year.;
   where NAME = "New Jersey" and age in (19,20,21,22) and sex = 0;
run;
proc means data = pop.&i_file. noprint;
   var POPESTIMATE&year.;
   output out = junk8 sum = all_WI&year.;
   where NAME = "Wisconsin" and age in (19,20,21,22) and sex = 0;
run;
*These are one row files so no need to merge on anything;
data pop&year. (drop = _type_ _freq_);
   merge junk1 junk2 junk3 junk4 junk5 junk6 junk7 junk8;
run;
*Clean up;
proc datasets;
   delete junk1 junk2 junk3 junk4 junk5 junk6 junk7 junk8;
run;
%mend;

%pop_c(2004,Sc_est2009_agesex_res,XXXXXXX,New Jersey,Wisconsin);
%pop_c(2005,Sc_est2009_agesex_res,Arizona,New Jersey,Wisconsin);
%pop_c(2006,Sc_est2009_agesex_res,Arizona,New Jersey,Wisconsin);
%pop_c(2007,Sc_est2009_agesex_res,Arizona,New Jersey,Wisconsin);
%pop_c(2008,Sc_est2009_agesex_res,Arizona,New Jersey,Wisconsin);
%pop_c(2009,Sc_est2009_agesex_res,Arizona,New Jersey,Wisconsin);
%pop_c(2010,Sc_est2011_agesex_res,XXXXXXX,New Jersey,Wisconsin);

*Check the time series - look fine;
data stability;
   set pop2004 (rename =(all2004 = all all_AZ2004 = all_AZ  all_NJ2004 = all_NJ  all_WI2004 = all_WI male2004 = male)) 
       pop2005 (rename =(all2005 = all all_AZ2005 = all_AZ  all_NJ2005 = all_NJ  all_WI2005 = all_WI male2005 = male))  
       pop2006 (rename =(all2006 = all all_AZ2006 = all_AZ  all_NJ2006 = all_NJ  all_WI2006 = all_WI male2006 = male))  
       pop2007 (rename =(all2007 = all all_AZ2007 = all_AZ  all_NJ2007 = all_NJ  all_WI2007 = all_WI male2007 = male))  
       pop2008 (rename =(all2008 = all all_AZ2008 = all_AZ  all_NJ2008 = all_NJ  all_WI2008 = all_WI male2008 = male))  
       pop2009 (rename =(all2009 = all all_AZ2009 = all_AZ  all_NJ2009 = all_NJ  all_WI2009 = all_WI male2009 = male))  
       pop2010 (rename =(all2010 = all                      all_NJ2010 = all_NJ  all_WI2010 = all_WI male2010 = male)) ;
run;

*Combine the population counts;
data inter.alc_pop_combined (keep = merge_v pop_all_AZ_NJ_WI pop_female_AZ_NJ_WI pop_male_AZ_NJ_WI pop_AZ pop_CA pop_IA pop_NJ pop_WI);
   merge pop2004 pop2005 pop2006 pop2007 pop2008 pop2009 pop2010;
  *To put this in per 10,000 person years of risk divide by 4 because 
   the population includes both 19, 20, 21 and 22 year olds and we want to put this in person 
   years. Divide by 12 because each person contributes only 1/12th of a year to each
   month observations.;
   pop_all_AZ_NJ_WI = (all2004 + all2005 + all2006 + all2007 + all2008 + all2009 + all2010)/(4*12);
   pop_female_AZ_NJ_WI = (female2004 + female2005 + female2006 + female2007 + female2008 + female2009 + female2010)/(4*12);
   pop_male_AZ_NJ_WI = (male2004 + male2005 + male2006 + male2007 + male2008 + male2009 + male2010)/(4*12);
  *By state include only the years included in the files;
   pop_AZ = (             all_AZ2005 + all_AZ2006 + all_AZ2007 + all_AZ2008 + all_AZ2009)/(4*12);
   pop_CA = (             all_CA2005 + all_CA2006 + all_CA2007                          )/(4*12);
   pop_IA = (all_IA2004 + all_IA2005 + all_IA2006 + all_IA2007 + all_IA2008 + all_IA2009)/(4*12);
   pop_NJ = (all_NJ2004 + all_NJ2005 + all_NJ2006 + all_NJ2007 + all_NJ2008 + all_NJ2009 + all_NJ2010)/(4*12);
   pop_WI = (all_WI2004 + all_WI2005 + all_WI2006 + all_WI2007 + all_WI2008 + all_WI2009 + all_WI2010)/(4*12);
   merge_v = 1; *To merge onto the count files;
run;

*Direct list file back to lst window;
PROC PRINTTO PRINT=PRINT;
RUN; 

*Direct log file back to log window;
PROC PRINTTO LOG=LOG;
RUN; 
